package com.kch.soundgame.gameplay;

import java.text.DecimalFormat;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.text.Text;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import android.graphics.Typeface;

/**
 * This abstract class sets the engine options, create resource and scenes.
 * It has abstract methods that must be implemented by its subclass
 * 
 * @author Chuan
 *
 */
public abstract class AudioCaptureGameActivity extends SimpleBaseGameActivity implements
		IUpdateHandler {

	public static final int CAMERA_WIDTH = 800;
	public static final int CAMERA_HEIGHT = 480;

	protected Camera camera;
	protected Scene scene;
	protected Font font;
	protected Text text;

	private SoundMeter mSensor = new SoundMeter();

	@Override
	public EngineOptions onCreateEngineOptions() {
		camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		EngineOptions engineOptions = new EngineOptions(true,
				ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),
				camera);
		return engineOptions;
	}

	@Override
	protected void onCreateResources() {
		font = FontFactory.create(this.getFontManager(),
				this.getTextureManager(), 256, 256,
				Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32);
		font.load();
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		onGameCreateResources();
	}

	@Override
	protected Scene onCreateScene() {
		scene = new Scene();
		scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
		onGameCreateScene();
		scene.registerUpdateHandler(this);
		return scene;
	}

	@Override
	public void onUpdate(float pSecondsElapsed) {
		DecimalFormat decim = new DecimalFormat("0.00");
		Double amp = Double.parseDouble(decim.format(mSensor.getAmp()));
		onGameUpdate(pSecondsElapsed, amp);
	}

	@Override
	public void reset() {
		// TODO Auto-generated method stub
		onGameReset();
	}

	@Override
	public void onResume() {
		super.onResume();
		mSensor.start();
	}

	@Override
	public void onStop() {
		super.onStop();
		mSensor.stop();

	}

	public abstract void onGameCreateScene();

	public abstract void onGameUpdate(float pSecondsElapsed, double amp);

	public abstract void onGameReset();
	
	public abstract void onGameCreateResources();
}